package org.hentai.tri.grpc.service;

import org.apache.dubbo.common.stream.StreamObserver;
import org.hentai.tri.grpc.DubboHentaiTriple;
import org.hentai.tri.grpc.HentaiReply;
import org.hentai.tri.grpc.HentaiRequest;

public class HentaiTriImpl extends DubboHentaiTriple.HentaiImplBase {


    public HentaiTriImpl() {
    }

    @Override
    public HentaiReply hentai(HentaiRequest request) {
        System.out.printf("Server received greet request %s\n", request);
        return HentaiReply.newBuilder()
                .setMessage(String.format("Hello Kora Hentai %s", request.getName()))
                .build();
    }

    @Override
    public StreamObserver<HentaiRequest> biStream(StreamObserver<HentaiReply> responseObserver) {
        return new StreamObserver<>() {
            @Override
            public void onNext(HentaiRequest data) {
                HentaiReply resp = HentaiReply.newBuilder()
                        .setMessage(String.format("Hello Kora Hentai %s", data.getName()))
                        .build();
                responseObserver.onNext(resp);
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {

            }
        };
    }

    @Override
    public void serverSteam(HentaiRequest request, StreamObserver<HentaiReply> responseObserver) {
        System.out.printf("receive request: %s\n", request.getName());
        for (int i = 0; i < 10; i++) {
            HentaiReply resp = HentaiReply.newBuilder()
                    .setMessage(String.format("Hello Kora Hentai %s. num %d", request.getName(), i))
                    .build();
            responseObserver.onNext(resp);
        }

        responseObserver.onCompleted();

    }
}
